package rexsee.core.notification;

import java.io.File;

import android.app.Notification;
import android.net.Uri;

public class NotificationArgumentsSheet {

	public String argumentsText = "";

	public String id = "auto";

	public String ticker = "";
	public String title = "";
	public String message = "";
	public String url = "";

	public String timeplus = "0";
	public String soundpath = "";

	public boolean cancelable = true;
	public boolean autocancel = true;
	public boolean insistent = false;
	public boolean onlyalertonce = false;
	public boolean showlights = true;

	public boolean sounds = true;
	public boolean lights = true;
	public boolean vibrate = false;

	public NotificationArgumentsSheet parseArguments(String arguments) {
		if (arguments == null || arguments.equals("")) return this;
		argumentsText = arguments;
		String[] argus = arguments.split(";");
		for (int i = 0; i < argus.length; i++) {
			if (argus[i].indexOf("=") <= 0) continue;
			String key = argus[i].substring(0, argus[i].indexOf("=")).toLowerCase().trim();
			String value = argus[i].substring(argus[i].indexOf("=") + 1).trim();
			if (value.length() == 0) continue;
			Boolean booleanValue = (value.equalsIgnoreCase("true")) ? true : false;
			try {
				this.getClass().getField(key).setBoolean(this, booleanValue);
			} catch (Exception e) {
				try {
					this.getClass().getField(key).set(this, value);
				} catch (Exception ee) {
				}
			}
		}
		return this;
	}

	public int getFlags() {
		int flags = 0;
		if (insistent) flags = flags | Notification.FLAG_INSISTENT;
		if (!cancelable) flags = flags | Notification.FLAG_NO_CLEAR;
		if (autocancel) flags = flags | Notification.FLAG_AUTO_CANCEL;
		if (onlyalertonce) flags = flags | Notification.FLAG_ONLY_ALERT_ONCE;
		if (showlights) flags = flags | Notification.FLAG_SHOW_LIGHTS;
		return flags;
	}
	public int getDefaults() {
		int defaults = 0;
		if (sounds) defaults = defaults | Notification.DEFAULT_SOUND;
		if (vibrate) defaults = defaults | Notification.DEFAULT_VIBRATE;
		if (lights) defaults = defaults | Notification.DEFAULT_LIGHTS;
		return defaults;
	}
	public Uri getSound() {
		if (soundpath.equals("")) return null;
		if (!soundpath.toLowerCase().startsWith("file://")) return null;
		Uri uri = Uri.parse(soundpath);
		File file = new File(uri.getPath());
		return (file.exists() && file.isFile() && file.canRead()) ? uri : null;
	}
	public long getWhen() {
		try {
			return System.currentTimeMillis() + (Integer.parseInt(timeplus) * 1000);
		} catch (Exception e) {
			return System.currentTimeMillis();
		}
	}
	public int getId() {
		return getId((int) getWhen());
	}
	public int getId(int defaultValue) {
		try {
			return Integer.parseInt(id);
		} catch (Exception e) {
			return defaultValue;
		}
	}

	//***Not used in notification, instead, used in Push, Alarm and RemoteCommand
	public String command = "notification";
	public String alermname = "defaultAlerm";
	public String alermfirsttime = "0";
	public String alermrepeatinterval = "-1";
	public String startapplicationurl = "rexsee:info";
	public boolean notificationimmediately = false;
	public boolean notificationafterexec = false;
	public boolean forcerepeat = false;

	public long getAlermFirstTime() {
		try {
			return Long.parseLong(alermfirsttime);
		} catch (Exception e) {
			return -1;
		}
	}
	public long getAlermRepeatInterval() {
		if (alermrepeatinterval.equals("")) return -1;
		try {
			return Integer.parseInt(alermrepeatinterval);
		} catch (Exception e) {
			return -1;
		}
	}

	//***

}
